//
//  SectionSliderVC.m
//  New_Runway
//
//  Created by mack on 2018/2/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "SectionSliderVC.h"
#import "ZPSlider.h"
#import "NRWCarChooseItemView.h"

@interface SectionSliderVC ()<NRWCarChooseItemViewDelegate>
@property (nonatomic, copy) NSString *fontIndex;
@property (nonatomic, copy) NSString *lastIndex;
@property (nonatomic, strong) NSArray *keyArray;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, assign) NSInteger sfirstIndex;
@property (nonatomic, assign) NSInteger slastIndex;
@property (nonatomic, strong) ZPSlider *slider;
@property (nonatomic, strong) NRWCarChooseItemView *chooseHotPriceView;
@end

@implementation SectionSliderVC
{
    UILabel *label;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"价格";
    [self.navigationBarView.rightItem setTitle:@"确定" forState:UIControlStateNormal];
    self.view.backgroundColor= kColorWhite();
    
    _keyArray = @[@"0",@"5",@"15",@"20",@"30",@"50",@"100",@"不限"];

        if (self.isWhich == YES) {
            NSString *priceText;
            if ([self.fromPrice isEqualToString:@""] && [self.toPrice isEqualToString:@""]) {
                priceText = @"不限";
            }else if (![self.fromPrice isEqualToString:@""] && ![self.toPrice isEqualToString:@""]){
                priceText = [NSString stringWithFormat:@"%@~%@万",self.fromPrice,self.toPrice];
            }else if ([self.fromPrice isEqualToString:@"50"] && [self.toPrice isEqualToString:@""]){
                priceText = @"50万以上";
            }
            self.hotPriceIndex = [[self getHotPriceArray] indexOfObject:priceText];
            self.sfirstIndex = 0;
            self.slastIndex = 7;
        }else{
            
            self.hotPriceIndex = [self getHotPriceArray].count + 1;
            if ([self.fromPrice isEqualToString:@""] && [self.toPrice isEqualToString:@""]) {
                self.slastIndex = 0;
                self.slastIndex = 7;
            }else if (![self.fromPrice isEqualToString:@""] && ![self.toPrice isEqualToString:@""]){
                self.sfirstIndex = [_keyArray indexOfObject:self.fromPrice];
                self.slastIndex = [_keyArray indexOfObject:self.toPrice];
            }else if ([self.fromPrice isEqualToString:@""] && ![self.toPrice isEqualToString:@""]){
                self.sfirstIndex = 0;
                self.slastIndex = [_keyArray indexOfObject:self.toPrice];
            }else if (![self.fromPrice isEqualToString:@""] && [self.toPrice isEqualToString:@""]){
                self.sfirstIndex = [_keyArray indexOfObject:self.fromPrice];
                self.slastIndex = 7;
            }
        }
    
    NRWCarChooseItemView *chooseHotPriceView = [[NRWCarChooseItemView alloc] init];
    chooseHotPriceView.itemTitleArr = [self getHotPriceArray];
    chooseHotPriceView.itemNumInRow = 4;
    chooseHotPriceView.margin = 14.5;
    chooseHotPriceView.marginSides = 15.5;
    chooseHotPriceView.marginUpDown = 15;
    chooseHotPriceView.titleColor = getColorFromString(@"ff6816");
    chooseHotPriceView.selectTitleColor = [UIColor whiteColor];
    chooseHotPriceView.normalImage = @"btn_orange_normal";
    chooseHotPriceView.selectImage = @"btn_orange_select";
    chooseHotPriceView.showBorder = NO;
    chooseHotPriceView.delegate = self;
    [chooseHotPriceView prepare];
    chooseHotPriceView.selectIndex = self.hotPriceIndex;
    self.view.addSubview(chooseHotPriceView);
    [chooseHotPriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBarView.mas_bottom).offset(20);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(chooseHotPriceView.chooseViewHeight));
    }];
    self.chooseHotPriceView = chooseHotPriceView;
    
    
    label = [[UILabel alloc] init];
    label.text = @"价格 （单位/万）";
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = MAIN_FONT_COLOR();
    [self.view addSubview:label];
    kSelfWeak;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.view).mas_offset(16);
//        make.top.equalTo(weakSelf.view).mas_offset(self.navH + 30);
        make.top.equalTo(chooseHotPriceView.mas_bottom).offset(30);
    }];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.textColor = MAIN_NAV_COLOR();
    self.priceLabel.font = [UIFont systemFontOfSize:16];
    self.priceLabel.text = @"不限";
    [self.view addSubview:_priceLabel];
    [weakSelf.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).mas_offset(3);
        make.centerY.equalTo(label);
    }];
    
    self.fontIndex = self.fromPrice;
    self.lastIndex = self.toPrice;
    
   [self PriceSlider:self.sfirstIndex withLastIndex:self.slastIndex];
}

-(void)PriceSlider:(NSInteger)firstIndex withLastIndex:(NSInteger)lastIndex{
    ZPSlider *slider=[[ZPSlider alloc] initWithFrame:CGRectMake(30, self.navH + 90, Screen_width - 60, 50) sliderTitles:_keyArray defaultFirstIndex:firstIndex defaultLastIndex:lastIndex sliderImage:[UIImage imageNamed:@"circle_s"]];
    [self.view addSubview:slider];    
    slider.backgroundColor = [UIColor clearColor];
    //    slider.block=^(int index){
    //        NSLog(@"当前index==%d",index);
    //    };
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(20);
        make.width.equalTo(@(Screen_width - 60));
        make.height.equalTo(@50);
        make.centerX.equalTo(self.view.mas_centerX);
    }];
    self.slider = slider;
    kSelfWeak;
    slider.block = ^(int firstIndexPath, int lastIndexPath) {
        if (firstIndexPath == 0 && lastIndexPath == 7) {
            weakSelf.priceLabel.text = @"不限";
            weakSelf.fontIndex = @"";
            weakSelf.lastIndex = @"";
        }
        if (firstIndexPath == 0 && lastIndexPath != 7) {
            
            weakSelf.priceLabel.text = [NSString stringWithFormat:@"%@万以内",weakSelf.keyArray[lastIndexPath]];
            weakSelf.fontIndex = @"";
            weakSelf.lastIndex = weakSelf.keyArray[lastIndexPath];
        }
        if (firstIndexPath != 0 && lastIndexPath != 7) {
            weakSelf.priceLabel.text = [NSString stringWithFormat:@"%@ ~ %@万",weakSelf.keyArray[firstIndexPath],weakSelf.keyArray[lastIndexPath]];
            weakSelf.fontIndex =  weakSelf.keyArray[firstIndexPath];
            weakSelf.lastIndex = weakSelf.keyArray[lastIndexPath];
       
        }
        if (firstIndexPath != 0 && lastIndexPath == 7) {
             weakSelf.priceLabel.text = [NSString stringWithFormat:@"%@万以上",weakSelf.keyArray[firstIndexPath]];
            weakSelf.fontIndex =  weakSelf.keyArray[firstIndexPath];
            weakSelf.lastIndex = @"";
            
        }
        weakSelf.isWhich = NO;
        weakSelf.chooseHotPriceView.selectIndex = [self getHotPriceArray].count + 1;
    };
 

}
-(void)rightItemClick{
   
   
    self.block(self.fontIndex, self.lastIndex, self.isWhich);
     [self.navigationController popViewControllerAnimated:YES];
}

- (void)chooseItemView:(NRWCarChooseItemView *)view itemDidClickWithItem:(UIButton *)sender andItemIndex:(NSInteger)itemIndex {
    
    NSString *fromPrice = nil;
    NSString *toPrice = nil;
    self.isWhich = YES;
    self.slider.firstSelectedInedx = 0;
    self.slider.lastSelectedInedx = 7;
    switch (itemIndex) {
        case 0:
            fromPrice = @"";
            toPrice = @"";
            break;
        case 1:
            fromPrice = @"0";
            toPrice = @"3";
            break;
        case 2:
            fromPrice = @"3";
            toPrice = @"5";
            break;
        case 3:
            fromPrice = @"5";
            toPrice = @"10";
            break;
        case 4:
            fromPrice = @"10";
            toPrice = @"15";
            break;
        case 5:
            fromPrice = @"15";
            toPrice = @"20";
            break;
        case 6:
            fromPrice = @"20";
            toPrice = @"30";
            break;
        case 7:
            fromPrice = @"30";
            toPrice = @"50";
            break;
        case 8:
            fromPrice = @"50";
            toPrice = @"";
            break;
        default:
            break;
    }
    
    if (self.block) {
        self.block(fromPrice, toPrice, self.isWhich);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSArray<NSString *>*)getHotPriceArray {
    
    return @[@"不限", @"0~3万", @"3~5万", @"5~10万", @"10~15万", @"15~20万", @"20~30万", @"30~50万", @"50万以上"];
}

@end
